#!/usr/bin/perl
# ezstor_local_group_chooser.cgi
# This CGI generated the HTML for choosing a group or list of groups.

require './web-lib.pl';
&init_config();
&ReadParse();
%access = &get_module_acl();

# Build list of primary groups
#setpwent();
#while(@uinfo = getpwent()) {
#	push(@{$members{$uinfo[3]}}, $uinfo[0]);
#	}
#endpwent() if ($gconfig{'os_type'} ne 'hpux');

if ($in{'multi'}) {
	# selecting multiple groups.
	if ($in{'frame'} == 0) {
		# base frame
		&PrintHeader();
		print "<script>\n";
		@ul = &split_quoted($in{'group'});
		$len = @ul;
		print "sel = new Array($len);\n";
		print "selr = new Array($len);\n";
		for($i=0; $i<$len; $i++) {
			print "sel[$i] = \"$ul[$i]\";\n";
			$tgp = &get_local_group($ul[$i]);
			@ginfo = getgrnam($ul[$i]);
			if ($tgp && @ginfo) {
				@mems = &unique( split(/ /, $ginfo[3]));
					#	 @{$members{$ginfo[2]}} );
				if (@mems > 3) { @mems = (@mems[0..1], "..."); }
				print "selr[$i] = \"",join(' ', @mems),"\";\n";
				}
			else { print "selr[$i] = \"???\";\n"; }
			}
		print "</script>\n";
		print "<title>$text{'groups_title1'}</title>\n";
		print "<frameset cols='50%,50%'>\n";
		print "<frame src=\"/ezstor_local_group_chooser.cgi?frame=1&multi=1\">\n";
		print "<frameset rows='*,40' frameborder=no>\n";
		print " <frame src=\"/ezstor_local_group_chooser.cgi?frame=2&multi=1\">\n";
		print " <frame src=\"/ezstor_local_group_chooser.cgi?frame=3&multi=1\" scrolling=no>\n";
		print "</frameset>\n";
		print "</frameset>\n";
		}
	elsif ($in{'frame'} == 1) {
		# list of all groups to choose from
		&header();
		print "<script>\n";
		print "function addgroup(u, r)\n";
		print "{\n";
		print "var i, find;\n";
		print "for (i = 0; i < top.sel.length; i++)\n";
		print "{\n";
		print "  if(top.sel[i] == u)\n";
		print "  {\n";
		print "  return false;";
		print "  }\n";
		print "}\n";
		print "top.sel[top.sel.length] = u\n";
		print "top.selr[top.selr.length] = r\n";
		print "top.frames[1].location = top.frames[1].location\n";
		print "return false;\n";
		print "}\n";
		print "</script>\n";
#		print "<font size=+1>$text{'groups_all'}</font>\n";
		print "<table width=100% class=mode1 cellspacing=0 cellpadding=2>\n";
		print "<tr><td colspan=2><b>", $text{'groups_all'}, "</b></td></tr>\n";
		print "<tr class=title1><td><b>", $text{'groups_gname'}, "</b></td><td><b>", $text{'groups_gmember'}, "</b></td></tr>\n";
		$ct = 0;
		foreach $u (&get_local_groups_list()) {
			if ($in{'group'} eq $u->[0]) { print "<tr class=title1>\n"; }
			else
			{
				if ($ct % 2)
				{
					$dismod = "class=mode2";
				}
				else
				{
					$dismod = "class=mode1";
				}
				print "<tr $dismod>\n";
			}
			$ct ++;
			print "<td width=20%><a href=\"\" onClick='return addgroup(\"$u->[0]\", \"$u->[3]\")'>$u->[0]</a></td>\n";
			print "<td>$u->[3]</td> </tr>\n";
			}
		print "</table>\n";
		}
	elsif ($in{'frame'} == 2) {
		# show chosen groups
		&header();
		print "<table width=100% class=mode1 cellspacing=0 cellpadding=2>\n";
		print "<tr><td colspan=2><b>", $text{'groups_sel'}, "</b></td></tr>\n";
		print "<tr class=title1><td><b>", $text{'groups_gname'}, "</b></td><td><b>", $text{'groups_gmember'}, "</b></td></tr>\n";
		print <<'EOF';

<script>
function sub(j)
{
sel2 = new Array(); selr2 = new Array();
for(k=0,l=0; k<top.sel.length; k++) {
	if (k != j) {
		sel2[l] = top.sel[k];
		selr2[l] = top.selr[k];
		l++;
		}
	}
top.sel = sel2; top.selr = selr2;
location = location;
return false;
}
for(i=0; i<top.sel.length; i++) {
	if (i % 2)
		document.write("<tr class=mode2>\n");
	else
		document.write("<tr class=mode1>\n");
	document.write("<td><a href=\"\" onClick='return sub("+i+")'>"+top.sel[i]+"</a></td>\n");
	document.write("<td>"+top.selr[i]+"</td>\n");
	}
</script>
</table>
EOF
		}
	elsif ($in{'frame'} == 3) {
		# output OK and Cancel buttons
		&header();
		print "<body onload='top.ibutton.disabled=true;' ";
		print " onunload='top.ibutton.disabled=false;'>\n";
		print "<script>\n";
		print "function qjoin(l)\n";
		print "{\n";
		print "rv = \"\";\n";
		print "for(i=0; i<l.length; i++) {\n";
		print "    if (rv != '') rv += ' ';\n";
		print "    if (l[i].indexOf(' ') < 0) rv += l[i];\n";
		print "    else rv += '\"'+l[i]+'\"'\n";
		print "    }\n";
		print "return rv;\n";
		print "}\n";
		print "</script>\n";
		print "<form>\n";
		print "&nbsp;&nbsp;<input type=button class=fbutton value=\"$text{'groups_ok'}\" ",
		      " onClick='top.ifield.value = qjoin(top.sel); ",
		      " top.close()'>\n";
		print "<input type=button class=fbutton value=\"$text{'groups_cancel'}\" ",
		      "onClick='top.ibutton.disabled=false; top.close()'>\n";
		print "&nbsp;<input type=button class=fbutton value=\"$text{'groups_clear'}\" onClick='top.sel = new Array(); top.selr = new Array(); top.frames[1].location = top.frames[1].location'>\n";
		print "</form>\n";
		}
	}
else {
	# selecting just one group .. display a list of all groups to choose from
	&header();
	print "<body onload='ibutton.disabled=true;' ";
	print " onunload='ibutton.disabled=false;'>\n";
	print "<script>\n";
	print "function select(f)\n";
	print "{\n";
	print "ifield.value = f;\n";
	print "top.close();\n";
	print "return false;\n";
	print "}\n";
	print "</script>\n";
	print "<title>$text{'groups_title2'}</title>\n";
	print "<table width=100% class=mode1 cellspacing=0 cellpadding=2>\n";
#	print "<tr><td colspan=2><b>", $text{'groups_all'}, "</b></td></tr>\n";
	print "<tr class=title1><td><b>", $text{'groups_gname'}, "</b></td><td><b>", $text{'groups_gmember'}, "</b></td></tr>\n";
	$ct = 0;
	foreach $u (&get_local_groups_list()) {
		if ($ct % 2)
		{
			$dismod = "class=mode2";
		}
		else
		{
			$dismod = "class=mode1";
		}
		print "<tr $dismod>\n";

		$ct ++;
		print "<td width=20%><a href='javascript:select(\"$u->[0]\")'>$u->[0]</a></td>\n";
		print "<td>$u->[3]</td> </tr>\n";
		}
	print "</table>\n";
	}



sub get_local_groups_list
{
local(@ginfo, @groups);
open(LG, "/etc/localgroup");
while($line = <LG>) {
	$line =~ s/\r|\n//g;
	@ginfo = getgrnam($line);
	@mems = unique( split(/ /, $ginfo[3])); #, @{$members{$ginfo[2]}});
	if (@mems > 3) { @mems = (@mems[0..1], "...");}
	$ginfo[3] = join (' ', @mems);
	push(@groups, [ @ginfo ]);
}
close(LG);
return sort { $a->[0] cmp $b->[0] } @groups;
}

sub get_local_group
{
local $group = undef;
open(LG, "/etc/localgroup");
while($line = <LG>) {
	$line =~ s/\r|\n//g;
	if ($line eq $_[0])
	{
		$group = $line;
		break;
	}
}
close(LG);
return $group;

}

# split_quoted(string)
sub split_quoted
{
local @rv;
local $str = $_[0];
while($str =~ /^\s*(\S*"[^"]+"\S*)(.*)$/ || $str =~ /^\s*(\S+)(.*)$/) {
	$str = $2;
	local $g = $1;
	$g =~ s/"//g;
	push(@rv, $g);
	}
return @rv;
}

